<?php

/**
 * StockArticle form base class.
 *
 * @method StockArticle getObject() Returns the current form's model object
 *
 * @package    SdCommerce
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseStockArticleForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                   => new sfWidgetFormInputHidden(),
      'article_id'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Article'), 'add_empty' => false)),
      'departement_id'       => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Departement'), 'add_empty' => false)),
      'quantite_min'         => new sfWidgetFormInputText(),
      'quantite_max'         => new sfWidgetFormInputText(),
      'quantite_reservee'    => new sfWidgetFormInputText(),
      'stock_reel'           => new sfWidgetFormInputText(),
      'stock_theorique'      => new sfWidgetFormInputText(),
      'casse'                => new sfWidgetFormInputText(),
      'perte'                => new sfWidgetFormInputText(),
      'gratuit'              => new sfWidgetFormInputText(),
      'gain'                 => new sfWidgetFormInputText(),
      'created_at'           => new sfWidgetFormDateTime(),
      'updated_at'           => new sfWidgetFormDateTime(),
      'm_bon_sortie_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'BonSortie')),
      'm_bon_entre_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'BonEntre')),
      'm_bon_transfert_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'BonTransfert')),
      'bon_sorties_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'BonSortie')),
    ));

    $this->setValidators(array(
      'id'                   => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'article_id'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Article'))),
      'departement_id'       => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Departement'))),
      'quantite_min'         => new sfValidatorInteger(),
      'quantite_max'         => new sfValidatorInteger(),
      'quantite_reservee'    => new sfValidatorInteger(),
      'stock_reel'           => new sfValidatorInteger(),
      'stock_theorique'      => new sfValidatorInteger(),
      'casse'                => new sfValidatorInteger(),
      'perte'                => new sfValidatorInteger(),
      'gratuit'              => new sfValidatorInteger(),
      'gain'                 => new sfValidatorInteger(),
      'created_at'           => new sfValidatorDateTime(),
      'updated_at'           => new sfValidatorDateTime(),
      'm_bon_sortie_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'BonSortie', 'required' => false)),
      'm_bon_entre_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'BonEntre', 'required' => false)),
      'm_bon_transfert_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'BonTransfert', 'required' => false)),
      'bon_sorties_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'BonSortie', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('stock_article[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'StockArticle';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['m_bon_sortie_list']))
    {
      $this->setDefault('m_bon_sortie_list', $this->object->MBonSortie->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['m_bon_entre_list']))
    {
      $this->setDefault('m_bon_entre_list', $this->object->MBonEntre->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['m_bon_transfert_list']))
    {
      $this->setDefault('m_bon_transfert_list', $this->object->MBonTransfert->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['bon_sorties_list']))
    {
      $this->setDefault('bon_sorties_list', $this->object->BonSorties->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveMBonSortieList($con);
    $this->saveMBonEntreList($con);
    $this->saveMBonTransfertList($con);
    $this->saveBonSortiesList($con);

    parent::doSave($con);
  }

  public function saveMBonSortieList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_bon_sortie_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MBonSortie->getPrimaryKeys();
    $values = $this->getValue('m_bon_sortie_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MBonSortie', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MBonSortie', array_values($link));
    }
  }

  public function saveMBonEntreList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_bon_entre_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MBonEntre->getPrimaryKeys();
    $values = $this->getValue('m_bon_entre_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MBonEntre', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MBonEntre', array_values($link));
    }
  }

  public function saveMBonTransfertList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['m_bon_transfert_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->MBonTransfert->getPrimaryKeys();
    $values = $this->getValue('m_bon_transfert_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('MBonTransfert', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('MBonTransfert', array_values($link));
    }
  }

  public function saveBonSortiesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['bon_sorties_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->BonSorties->getPrimaryKeys();
    $values = $this->getValue('bon_sorties_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('BonSorties', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('BonSorties', array_values($link));
    }
  }

}
